/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.LagerHandler;
import javax.swing.JLabel;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Morten J, Morten B & Jonas
 */
public class Lager extends javax.swing.JPanel {

    private DefaultTableModel model;
    public static LagerHandler lh;
    private RowSorter<DefaultTableModel> sorter;
    private String sString;

    /**
     * Creates new form Lager
     */
    public Lager() {

        initComponents();

        sString = jSearch.getText();

        model = (DefaultTableModel) jVareTable.getModel();
        sorter = new TableRowSorter<>(model);
        jVareTable.setRowSorter(sorter);
        jVareTable.getColumnModel().getColumn(0).setPreferredWidth(195);
        jVareTable.getColumnModel().getColumn(1).setPreferredWidth(145);

        lh = new LagerHandler(model);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanSearch = new javax.swing.JPanel();
        jSearch = new javax.swing.JTextField();
        jPanVareListe = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jVareTable = new javax.swing.JTable();
        jFeedback = new javax.swing.JLabel();

        setBackground(new java.awt.Color(186, 153, 108));
        setPreferredSize(new java.awt.Dimension(400, 400));

        jPanSearch.setBackground(new java.awt.Color(186, 153, 108));
        jPanSearch.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(240, 240, 240)), "Søg varenavn", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Verdana", 0, 11), java.awt.Color.darkGray)); // NOI18N

        jSearch.setBackground(new java.awt.Color(240, 240, 240));
        jSearch.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        jSearch.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(250, 250, 250)));
        jSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jSearchKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanSearchLayout = new javax.swing.GroupLayout(jPanSearch);
        jPanSearch.setLayout(jPanSearchLayout);
        jPanSearchLayout.setHorizontalGroup(
            jPanSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanSearchLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanSearchLayout.setVerticalGroup(
            jPanSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanSearchLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanVareListe.setBackground(new java.awt.Color(186, 153, 108));
        jPanVareListe.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(240, 240, 240)), "Vareliste", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Verdana", 0, 11), java.awt.Color.darkGray)); // NOI18N

        jVareTable.setBackground(new java.awt.Color(206, 173, 128));
        jVareTable.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(250, 250, 250)));
        jVareTable.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jVareTable.setForeground(new java.awt.Color(25, 25, 25));
        jVareTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Navn", "Varegruppe", "Varenr.", "Kr.", "Euro", "Antal"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jVareTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jVareTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jVareTable.setGridColor(new java.awt.Color(250, 250, 250));
        jVareTable.setName(""); // NOI18N
        jVareTable.setSelectionBackground(new java.awt.Color(240, 240, 240));
        jVareTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jVareTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jVareTableMouseClicked(evt);
            }
        });
        jVareTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jVareTableKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jVareTable);

        javax.swing.GroupLayout jPanVareListeLayout = new javax.swing.GroupLayout(jPanVareListe);
        jPanVareListe.setLayout(jPanVareListeLayout);
        jPanVareListeLayout.setHorizontalGroup(
            jPanVareListeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanVareListeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanVareListeLayout.setVerticalGroup(
            jPanVareListeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanVareListeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jFeedback.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        jFeedback.setForeground(new java.awt.Color(34, 104, 1));
        jFeedback.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jFeedback.setMaximumSize(new java.awt.Dimension(21, 15));
        jFeedback.setMinimumSize(new java.awt.Dimension(21, 15));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanVareListe, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jFeedback, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(jFeedback, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(8, 8, 8)
                .addComponent(jPanVareListe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public String getsString() {
        return sString;
    }

    public void clearFeedback() {
        jFeedback.setText("");
    }

    public void setStringFocus() {
        jSearch.requestFocus();
        jSearch.selectAll();
    }

    private void jSearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jSearchKeyReleased
        sString = jSearch.getText();
        lh.searchStock(sString);
    }//GEN-LAST:event_jSearchKeyReleased

    private void jVareTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jVareTableMouseClicked
        tableTextSelected();
    }//GEN-LAST:event_jVareTableMouseClicked

    public void tableTextSelected() {
        if (jVareTable.getSelectedRow() > -1) {
            String vareNr = model.getValueAt(sorter.convertRowIndexToModel(jVareTable.getSelectedRow()), 2).toString();
            GUI.getGuiHandler().setIndtastetVare(vareNr);
            jFeedback.setText("Valgte varenummer: " + vareNr);
        }
    }

    private void jVareTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jVareTableKeyReleased
        tableTextSelected();
    }//GEN-LAST:event_jVareTableKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jFeedback;
    private javax.swing.JPanel jPanSearch;
    private javax.swing.JPanel jPanVareListe;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jSearch;
    private javax.swing.JTable jVareTable;
    // End of variables declaration//GEN-END:variables

    public JLabel getLabel() {
        return jFeedback;
    }
}
